package org.zebra.mybatis.plus.core;

import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ReflectUtil;
import org.dromara.streamquery.stream.plugin.mybatisplus.Database;
import org.zebra.mybatis.plus.util.MyBatisPlusUtil;

import javax.annotation.PostConstruct;

/**
 * 动态服务
 * @author zhanghongbin
 * @param <T> 泛型
 */
public abstract class DynamicServicePlusImpl<T> extends ServicePlusImpl {

    private IServicePlus<MapperPlus<T>, T> servicePlus;

    @PostConstruct
    @Override
    void initServicePlusImpl() {
        Class<T> entityClass = (Class<T>) ClassUtil.getTypeArgument(getClass(), 0);
        Class<?> mapperClass = Database.getMapperClass(entityClass);
        MapperPlus mapperPlus = MyBatisPlusUtil.getMapperPlus(mapperClass);
        this.servicePlus = new IServicePlus();
        ReflectUtil.setFieldValue(servicePlus, "baseMapper", mapperPlus);
        ReflectUtil.setFieldValue(servicePlus, "entityClass", entityClass);
        ReflectUtil.setFieldValue(servicePlus, "mapperClass", mapperClass);
        super.servicePlus = this.servicePlus;
    }

    @Override
    public IServicePlus<MapperPlus<T>, T> getServicePlus() {
        return this.servicePlus;
    }
}
